package com.rock.code.system.module.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.rock.code.base.service.impl.BaseServiceImpl;
import com.rock.code.common.pager.Page;
import com.rock.code.common.tree.AdditionalParameters;
import com.rock.code.common.tree.ItemNode;
import com.rock.code.system.module.dao.TbSysModuleDao;
import com.rock.code.system.module.domain.TbSysModule;
import com.rock.code.system.module.service.TbSysModuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理 Service 实现
 *
 * @author kongqiang
 * @date 2018/06/14
 */
@Service
public class TbSysModuleServiceImpl extends BaseServiceImpl<TbSysModuleDao> implements TbSysModuleService {

    @Resource
    private TbSysModuleDao tbSysModuleDao;

    /**
     * Insert.
     *
     * @param tbSysModule the TbSysModule
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public int insert(TbSysModule tbSysModule) throws Exception {
        tbSysModule.setModuleId(tbSysModule.getModuleCode());
        return tbSysModuleDao.insert(tbSysModule);
    }

    /**
     * Delete.
     *
     * @param primaryKey the primary key
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public int delete(String primaryKey) throws Exception {
        return tbSysModuleDao.delete(primaryKey);
    }

    /**
     * Delete by param.
     *
     * @param param the param
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public int deleteByParam(Map<String, Object> param) throws Exception {
        return tbSysModuleDao.deleteByParam(param);
    }

    /**
     * Update.
     *
     * @param tbSysModule the TbSysModule
     * @return the int
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public int update(TbSysModule tbSysModule) throws Exception {
        return tbSysModuleDao.update(tbSysModule);
    }

    @Override
    public TbSysModule query(TbSysModule tbSysModule) throws Exception {
        return tbSysModuleDao.query(tbSysModule);
    }

    /**
     * Query by para map.
     *
     * @param param the param
     * @return the tbSysModule
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public TbSysModule queryByParaMap(Map<String, Object> param) throws Exception {
        return tbSysModuleDao.queryByParaMap(param);
    }

    /**
     * Query list.
     *
     * @param tbSysModule the TbSysModule
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public List<TbSysModule> queryList(TbSysModule tbSysModule) throws Exception {
        return tbSysModuleDao.queryList(tbSysModule);
    }

    /**
     * Query list by para map.
     *
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public List<TbSysModule> queryListByParaMap(Map<String, Object> param) throws Exception {
        return tbSysModuleDao.queryListByParaMap(param);
    }

    /**
     * Query page list.
     *
     * @param param the param
     * @return the page info
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    @Override
    public PageInfo queryPageList(Page page, Map<String, Object> param) throws Exception {
        String orderBy = "";
        if (!StringUtil.isEmpty(page.getSidx())) {
            orderBy = this.getOrderBy(page);
        }
        PageHelper.startPage(page.getPage(), page.getRows());
        if (!orderBy.equals("")) {
            PageHelper.orderBy(orderBy);
        }
        List<TbSysModule> list = tbSysModuleDao.queryListByParaMap(param);
        return new PageInfo(list);
    }

     /**
     * Query list by order.
     *
     * @param page  the page
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    public List<TbSysModule> queryListByOrder(Page page, Map<String, Object> param) throws Exception {
        String orderBy = "";
        if (!StringUtil.isEmpty(page.getSidx())) {
            orderBy = this.getOrderBy(page);
        }
        if (!orderBy.equals("")) {
            param.put("orderBy", orderBy);
        }
        return tbSysModuleDao.queryListByParaMap(param);
    }

    /**
     * Query tree data.
     *
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    public List<ItemNode> queryTreeModule(Map<String, Object> param) throws Exception {
        List<TbSysModule> tbSysModules = tbSysModuleDao.queryUserRoleModuleByParaMap(param);
        List<ItemNode> itemNodes = new ArrayList<>();
        for (TbSysModule tbSysModule : tbSysModules){
            ItemNode itemNode = new ItemNode();
            itemNode.setText(tbSysModule.getModuleName());
            itemNode.setType(tbSysModule.getIsLeaf() == 1 ? "item" : "folder");
            AdditionalParameters additionalParameters = new AdditionalParameters();
            additionalParameters.setId(tbSysModule.getModuleCode());
            additionalParameters.setTreeLevel(tbSysModule.getTreeLevel());
            additionalParameters.setModulePath(tbSysModule.getModulePath());
            additionalParameters.setDataIcon("ace-icon fa fa-bars");
            itemNode.setAdditionalParameters(additionalParameters);
            itemNodes.add(itemNode);
        }
        return itemNodes;
    }

    /**
     * Query tree data.
     *
     * @param param the param
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2018/06/14
     */
    public List<ItemNode> queryTreeData(Map<String, Object> param) throws Exception {
        List<TbSysModule> tbSysModules = tbSysModuleDao.queryListByParaMap(param);
        List<ItemNode> itemNodes = new ArrayList<>();
        for (TbSysModule tbSysModule : tbSysModules){
            ItemNode itemNode = new ItemNode();
            itemNode.setText(tbSysModule.getModuleName());
            itemNode.setType(tbSysModule.getIsLeaf() == 1 ? "item" : "folder");
            AdditionalParameters additionalParameters = new AdditionalParameters();
            additionalParameters.setId(tbSysModule.getModuleCode());
            additionalParameters.setTreeLevel(tbSysModule.getTreeLevel());
            additionalParameters.setModulePath(tbSysModule.getModulePath());
            additionalParameters.setDataIcon("ace-icon fa fa-bars");
            itemNode.setAdditionalParameters(additionalParameters);
            itemNodes.add(itemNode);
        }
        return itemNodes;
    }

    @Override
    public List<TbSysModule> queryUserRoleModuleByParaMap(Map<String, Object> param) throws Exception {
        return tbSysModuleDao.queryUserRoleModuleByParaMap(param);
    }
}